/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.subareas.settings;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import softstarter.filehandling.FileCentre;
import softstarter.gui.master.GUIStateController;
import softstarter.gui.subareas.ButtonFactory;
import softstarter.launching.RunnableItem;


public class SettingsView {

    private GUIStateController masterController;
    private SettingsViewListener listener;
    
    private FileCentre fileCentre;
    private JPanel masterContainer;
    private HashMap<JButton, Object[]> filesAndButtons;
    
    private ButtonFactory buttonFactory;
    
    public SettingsView(GUIStateController masterController) {
        this.masterController = masterController;
        fileCentre = FileCentre.getInstance();
        buttonFactory = new ButtonFactory();
        
        listener = new SettingsViewListener(new SettingsViewController(this), this);
        build();
    }
    
    private void build() {

        masterContainer = new JPanel(new BorderLayout());
        
        buildListWithFilesAndRemoveButtons();
        
    }

    public JPanel getView() {
        return masterContainer;
    }

    public void buildListWithFilesAndRemoveButtons() {
        filesAndButtons = new HashMap<JButton, Object[]>();
        masterContainer.removeAll();
                
        JPanel filesAndButtonsContainer = new JPanel();
        filesAndButtonsContainer.setLayout(new BoxLayout(filesAndButtonsContainer, BoxLayout.Y_AXIS));
        filesAndButtonsContainer.setAlignmentX(Component.LEFT_ALIGNMENT);
        
//        filesAndButtonsContainer.setBorder(BorderFactory.createTitledBorder("Files and folders in the index"));
        
        filesAndButtonsContainer.setName("Files and folders in the index");
        
        for(RunnableItem item : fileCentre.getFiles()) {
            
            JPanel nameAndButtonPair = new JPanel();
            nameAndButtonPair.setLayout(new BoxLayout(nameAndButtonPair, BoxLayout.X_AXIS));
            nameAndButtonPair.setAlignmentX(Component.LEFT_ALIGNMENT);
            nameAndButtonPair.setMaximumSize(new Dimension(250, 30));
            
            JTextField fileName = new JTextField(item.getName());
            fileName.setPreferredSize(new Dimension(200, 30));
            JButton remove = buttonFactory.getNewIconButton("Remove", "./resources/TrashCan.png", "Remove");
            remove.addActionListener(listener);
            
            filesAndButtons.put(remove, new Object[] { fileName, item });
            
            nameAndButtonPair.add(fileName);
            nameAndButtonPair.add(remove);
            
            filesAndButtonsContainer.add(nameAndButtonPair);
            
        }
                
        JScrollPane filesAndButtonsContainerScroller = new JScrollPane(filesAndButtonsContainer);
        filesAndButtonsContainerScroller.setBorder(BorderFactory.createTitledBorder("Files and folders in the index"));
        filesAndButtonsContainerScroller.setPreferredSize(new Dimension(300, 300));
        
        masterContainer.add(filesAndButtonsContainerScroller, BorderLayout.CENTER);
        
        JPanel rightControlButtonsPanel = new JPanel();
        rightControlButtonsPanel.setLayout(new BoxLayout(rightControlButtonsPanel, BoxLayout.Y_AXIS));
//        rightControlButtonsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        
        JButton saveButton = new JButton("Save new filenames");//buttonFactory.getNewIconButton("Save", "./resources/SaveButton.png", "Save filenames");
        saveButton.setActionCommand("Save");
        saveButton.addActionListener(listener);

        JButton removeOldFilesButton = new JButton("Remove moved/deleted files");
        removeOldFilesButton.setActionCommand("Remove old files");
        removeOldFilesButton.addActionListener(listener);
        
        JButton removeAllButton = new JButton("Remove all files");
        removeAllButton.setActionCommand("Remove all files");
        removeAllButton.addActionListener(listener);

        JButton indexAFolder = new JButton("Index a folder...");
        indexAFolder.setActionCommand("Index a folder");
        indexAFolder.addActionListener(listener);

        rightControlButtonsPanel.add(saveButton);
        rightControlButtonsPanel.add(removeOldFilesButton);
        rightControlButtonsPanel.add(removeAllButton);
//        rightControlButtonsPanel.add(indexAFolder);

        masterContainer.add(rightControlButtonsPanel, BorderLayout.EAST);

        
        JPanel hintPanel = new JPanel();
        hintPanel.setLayout(new BoxLayout(hintPanel, BoxLayout.Y_AXIS));
        
        hintPanel.add(new JLabel("Tips:"));
        hintPanel.add(new JLabel("Use the hotkey \"win + s\""));
        
        masterContainer.add(hintPanel, BorderLayout.SOUTH);
        
        masterController.refreshGUI();
    }

    public HashMap<JButton, Object[]> getFilesAndButtons() {
        return filesAndButtons;
    }
}
